
package sUtils;

import javax.microedition.lcdui.*;
import java.util.*;

public class Menu {

    public boolean active=false, updated=false;
    int current=-1;
    Vector list = null;
    Image bg = null;
    int iw=0, ix=0, iy=0, ih=0, col=0, by=0, dy=0;

    public class Row
    {
        String text, id;
        int ix=-1,iy=-1,iw=0,ih=0, col=0,state=-1,bgcol=0xffffff, sepcol=0x808080;
        public Row( String s, String i, int x, int y, int w, int h, int c )
        {
            text=s; id=i;
            iw = w;
            ih = h;
            ix = x;
            iy = y;
            col =c;
            int gray = (((col>>16)&0xff) + ((col>>8)&0xff) + (col&0xff)) / 3;
            bgcol = gray>128 ? 0x202020 : 0xc0c0c0;
        }
        public boolean paint( Graphics g, int dy, boolean smallsep, boolean current )
        {
            boolean rc = false;
            g.setColor( (current && text!=null) ? col : bgcol );
            g.fillRect(ix, dy+iy, iw, ih);
            if (text==null)
            {
                g.setColor( sepcol );
                g.fillRect(ix+5, iy+dy+(ih-4)/2, iw-10, 2);
                g.setColor( 0xffffff );
                g.fillRect(ix+6, iy+dy+(ih-4)/2+2, iw-10, 1);
            }
            else
            {
                int sw = ih/3;
                if (smallsep)
                {
                    g.setColor( sepcol );
                    g.fillRect(ix+2,iy+dy+ih,iw-4,1);
                }
                g.setColor( current ? 0 : 0xffffff );
                g.drawString( text, ix+10+sw, iy+dy+(ih-g.getFont().getHeight())/2+2, 0);
                g.setColor( current ? bgcol : col );
                g.drawString( text, ix+9+sw, iy+dy+(ih-g.getFont().getHeight())/2+1, 0);
                if (state>=0)
                {
                    g.drawArc(ix+5, iy+(ih-sw)/2, sw, sw,0,360);
                    if (state>0) g.fillArc(ix+5+3, iy+(ih-sw)/2+3, sw-5, sw-5,0,360);
                }
                rc=true;
            }
            return rc;
        }
        public String hit( int x, int y )
        {
            return (x>=ix && y>=iy && x<(ix+iw) && y<(iy+ih)) ? id : null;
        }
        public void setState( int s )
        {
            state=s;
        }
        public boolean equals( String s )
        {
            return id!=null ? id.equalsIgnoreCase(s) : false;
        }
    }

    public void init( int w, int h, int c, boolean left )
    {
        ix = left?0:(w/2);
        by = iy = h;
        dy = 0;
        iw = w/2;
        ih = h/12;
        col = c;
        list = new Vector(10,1);
    }
    public void add( String s, String id )
    {
        int h=ih; if (s==null) h=ih/4;
        iy-=h;
        list.addElement( new Row(s,id,ix,iy+dy,iw,h,col) );
    }
    public void setState( String s, int state )
    {
        for ( int i=0; i<list.size(); i++ ) {
            Row r = (Row)(list.elementAt(i));
            if (r.equals(s)) r.setState(state);
        }
    }
    public void changeDim( int w, int h )
    {
        by=h;
    }
    public void show() { active=updated=true; current=-1; }
    //public void resize( int w,int h) { System.out.print(" Resize "+w+":"+h); if (ih+dy!=h) { dy=h-ih; } }
    public void paint( Graphics g )
    {
        if (active)
        {
            Font old = g.getFont();
            g.setFont( Font.getFont(Font.FONT_STATIC_TEXT));
            boolean smallsep = false;
            for ( int i=0; i<list.size(); i++ ) {
                Row r = (Row)(list.elementAt(i));
                smallsep = r.paint(g,dy,smallsep,current==i);
            }
            g.setColor(0x808080);
            g.drawRect(ix, iy+dy, iw, by-iy);
            g.setFont(old);
        }
        updated=false;
    }
    public String hit( int x, int y )
    {
        String ret = null;
        if (active)
            for ( int i=0; i<list.size() && ret==null; i++ )
            {
                Row r = (Row)(list.elementAt(i));
                if ( (ret = r.hit(x,y+dy))!=null )
                {
                    if (i!=current) updated=true;
                    current=i; break;
                }
            }
        return ret;
    }
    public void release()
    {
        active=false; updated=true; current=-1;
    }

}
